/**
 * (C) 2007 - Boston University
 * 	Contact: Michael Ocean 

  This work is a part of the larger snBench project.  Information
  can be found at: http://csr.bu.edu/snbench

  This program is free software; you can redistribute it and/or
  modify it under the terms of Version 2 of the GNU General Public 
  License.
  
  This library is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  General Public License for more details.
  
  You should have received a copy of the GNU Library General Public
  License along with this library; if not, write to the Free
  Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
**/
package snobject;

import snbenchlib.Util;
import snobject.snObject;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import step.parse.SyntaxError;

public class snPair extends snObject {

	
	/**
	 * instantiate a new snString with given content
	 * @param _content
	 */
	public snPair(snObject oLeft, snObject oRight) {
		super();
		
		snObject[] a = new snObject[2];
		a[0] = oLeft;
		a[1] = oRight;
		content = a;
	}

	public String getXmlMimeType() { return MimeTypes.PAIR; }
	public String getWebMimeType() { return "text/plain"; }
	public String getBinaryMimeType() { return "text/plain"; }
	
	public snObject getLeft(){
		snObject[] a = ((snObject[])content);
		return a[0];
	}

	public snObject getRight(){
		snObject[] a = ((snObject[])content);
		return a[1];
	}
	
	/* fix */
	public void xmlWrite(java.io.OutputStream out, int depth) throws java.io.IOException {
		xmlWritePre(out,depth);
		snObject[] a = ((snObject[])content);
		
		step.Node.Indent(out, depth);
		out.write("<left>".getBytes());
		a[0].xmlWrite(out,depth+1);
		out.write("</left>\n".getBytes());
		step.Node.Indent(out, depth);
		out.write("<right>".getBytes());
		a[1].xmlWrite(out,depth+1);
		out.write("</right>\n".getBytes());
		
		xmlWritePost(out,depth);
	}
	
	public void webWrite(java.io.OutputStream out, int depth) throws java.io.IOException {
		snObject[] a = ((snObject[])content);
		
		step.Node.Indent(out, depth);
		out.write("<left>\n".getBytes());
		a[0].webWrite(out,depth+1);
		out.write("\n".getBytes());
		step.Node.Indent(out, depth);
		out.write("</left>\n".getBytes());
		
		step.Node.Indent(out, depth);
		out.write("<right>\n".getBytes());
		a[1].webWrite(out,depth+1);
		out.write("\n".getBytes());
		step.Node.Indent(out, depth);
		out.write("</right>\n".getBytes());
	}

	public void binaryWrite(java.io.OutputStream out) throws java.io.IOException {
		xmlWrite(out,0);
	}
	
	public static snPair xmlParse(org.w3c.dom.Element e) 
	throws SyntaxError, MimeException {
		snObject snLeft = null;
		snObject snRight = null;
		if (!e.getTagName().equals("snobject"))
			return null;
		
		NodeList nl = e.getChildNodes();
		for(int i = 0; i < nl.getLength(); i++){
			Node n = nl.item(i);
			if (n.getNodeName().equalsIgnoreCase("left")){
				/* now we have to get the snObject inside this element */
				snLeft = snObject.xmlParse(getChildSNObject(n));
			}
			if (n.getNodeName().equalsIgnoreCase("right")){
				snRight = snObject.xmlParse(getChildSNObject(n));
			}
			else if(n.getNodeType() == org.w3c.dom.Node.TEXT_NODE) {
				/* ignore white space */
			}
		}
		if((snLeft==null)||(snRight==null)){
			System.err.println("Parsing failed for PAIR");
			System.exit(-1);
		}
		return new snPair(snLeft,snRight);
	}

}
